package com.synergy.stockflow.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.constant.ResultCode;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.Material;
import com.synergy.stockflow.product.entity.Process;
import com.synergy.stockflow.product.entity.ProcessStep;
import com.synergy.stockflow.product.service.OrderStepsService;
import com.synergy.stockflow.product.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/process")
public class ProcessController {
    @Autowired
    private ProcessService processService;
    @GetMapping("/index")
    public Result<Page<Process>> page(@RequestParam(name = "current", defaultValue = "1") int current,
                                         @RequestParam(name = "size", defaultValue = "5") int size,
                                      @RequestParam(name = "processRouteId", defaultValue = "") String processRouteId,
                                      @RequestParam(name = "processRouteName", defaultValue = "") String processRouteName,
                                      @RequestParam(name = "isEnabled", defaultValue = "") Integer isEnabled
    ){
        {
            System.out.println(isEnabled);
            QueryWrapper<Process> query =new QueryWrapper<>();

            if(processRouteName != null && processRouteName.length() > 0){
                query.lambda().like(Process::getProcessRouteName,processRouteName);
            }
            if(processRouteId != null && processRouteId.length() > 0){
                query.lambda().like(Process::getProcessRouteId,processRouteId);
            }
            if(isEnabled != null){
                query.lambda().like(Process::getIsEnabled,isEnabled);
            }
            query.lambda().orderBy(true,false, Process::getCreateTime);
            Page<Process> page = new Page<>(current,size);
            page = processService.page(page,query);
            return Result.success(page);
        }
    }
    @PostMapping("insert")
    public Result insert(@RequestBody Process process){
        QueryWrapper<Process> query = new QueryWrapper<>();
        if (process.getProcessRouteId() != null && process.getProcessRouteId().length() > 0) {
            query.lambda().eq(Process::getProcessRouteId, process.getProcessRouteId());
        }
        query.lambda().orderBy(true,false, Process::getCreateTime);
        Page<Process> page = new Page<>(1, 10000);
        Page<Process> page1 = processService.page(page, query);
        System.out.println(page1);
        if (page1 == null || page1.getRecords().size() == 0) {
            process.setCreateTime(new Date());
            process.setUpdateTime(new Date());
            processService.save(process);
            return Result.success(process);
        }
        return Result.error(ResultCode.BUSINESS_EXCEPTION.value(), "工艺路线编码已存在");

    }
    @PutMapping("update")
    public Result update(@RequestBody Process process){
        System.out.println(process);
        process.setUpdateTime(new Date());
        processService.updateById(process);
        return Result.success(process);
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<String> processIds){
        processService.removeBatchByIds(processIds);
        return Result.success(processIds);
    }
}
